﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace SPBusiness.ExtensionMethods
{
    public static class ExtSPList
    {


        public static SPView TryGetView(this SPList vSPList, string pTitle)
        {
            SPView vReturn = null;

            vReturn = vSPList.Views.OfType<SPView>().Where(x => x.Title == pTitle).FirstOrDefault();

            return vReturn;
        }

        public static SPListItem TryGetItemById(this SPList vSPList, int id)
        {
            SPListItem vReturn = null;
            SPQuery vSPQuery = new SPQuery();

            if (vSPList.BaseType == SPBaseType.DocumentLibrary)
            {
                vSPQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
            }

            vSPQuery.Query = string.Concat(
                          "<Where>",
                                "<Eq><FieldRef Name='ID'/><Value Type='Counter'>" + id.ToString() + "</Value></Eq>",
                          "</Where>");

            SPListItemCollection vSPListItemCollection = vSPList.GetItems(vSPQuery);

            if (vSPListItemCollection.Count > 0)
            {
                vReturn = vSPListItemCollection[0];
            }

            return vReturn;
        }

    }
}
